import { createRouter, createWebHistory } from 'vue-router';
import routerList from './list.js';

// 定义路由配置
const routes = [
    {
        path: '/',
        name: '首页',
        component: () => import('@/pages/homePage.vue'),
        redirect: routerList[0].path || "",
        children: routerList
    },
    {
        path: '/login',
        name: '登录',
        component: () => import('@/components/Login/index.vue'),
    },
];

// 创建路由实例
const router = createRouter({
    history: createWebHistory(),
    routes
});
// 全局前置守卫
router.beforeEach((to, from, next) => {
    const isLoggedIn = window.localStorage.getItem('token') ? true : false
    if (to.path !== '/login' && !isLoggedIn) {
        next('/login');
    } else if (to.path == '/login' && isLoggedIn) {
        next('/');
    }
    else {
        next();
    }
});

export default router;